/**
 * @description 检测对象是否有哪个类名
 * @param obj
 * @param classStr
 * @return {boolean}
 * @example hasClass(obj,'test')
 * @return true|false
 */
export let hasClass = (obj, classStr) => {
  return obj.className.split(/\s+/).indexOf(classStr) === -1 ? false : true
}
/**
 * @description 添加类名
 * @param obj
 * @param classStr
 * @return {ecDo}
 */
export let addClass = (obj, classStr) => {
  if ((this.isType(obj, 'array') || this.isType(obj, 'elements')) && obj.length >= 1) {
    for (let i = 0, len = obj.length; i < len; i++) {
      if (!this.hasClass(obj[i], classStr)) {
        obj[i].className += ' ' + classStr
      }
    }
  } else if (this.hasClass(obj, classStr)) {
    obj.className += ' ' + classStr
  }
  return this
}

/**
 * @description 删除类名
 * @param obj
 * @param classStr
 * @return {ecDo}
 */
export let removeClass = (obj, classStr) => {
  let reg
  if ((this.isType(obj, 'array') || this.isType(obj, 'elements')) && obj.length > 1) {
    for (let i = 0, len = obj.length; i < len; i++) {
      if (this.hasClass(obj[i], classStr)) {
        reg = new RegExp('(\\s|^)' + classStr + '(\\s|$)')
        obj[i].className = obj[i].className.replace(reg, '')
      }
    }
  } else if (this.hasClass(obj, classStr)) {
    reg = new RegExp('(\\s|^)' + classStr + '(\\s|$)')
    obj.className = obj.className.replace(reg, '')
  }
  return this
}
/**
 * @description 获取url参数
 * @param url
 * @return {{}}
 */
export let getUrlParam = url => {
  url = url ? url : window.location.href
  let _pa = url.substring(url.indexOf('?') + 1),
    _arrS = _pa.split('&'),
    _rs = {}
  for (let i = 0, _len = _arrS.length; i < _len; i++) {
    let pos = _arrS[i].indexOf('=')
    if (pos === -1) {
      continue
    }
    let name = _arrS[i].substring(0, pos)
    _rs[name] = window.decodeURIComponent(_arrS[i].substring(pos + 1))
  }
  return _rs
}

/**
 * @description 手机类型判断
 * @param type
 * @return {*}
 */
export let browserInfo = type => {
  switch (type) {
    case 'android':
      return navigator.userAgent.toLowerCase().indexOf('android') !== -1
    case 'iphone':
      return navigator.userAgent.toLowerCase().indexOf('iphone') !== -1
    case 'ipad':
      return navigator.userAgent.toLowerCase().indexOf('ipad') !== -1
    case 'weixin':
      return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1
    default:
      return navigator.userAgent.toLowerCase()
  }
}
//函数节流
// let count=0;
// function fn1(){
//     count++;
//     console.log(count)
// }
// //100ms内连续触发的调用，后一个调用会把前一个调用的等待处理掉，但每隔200ms至少执行一次
// document.onmousemove=ecDo.delayFn(fn1,100,200)
export function delayFn(fn, delay, mustDelay) {
  let timer = null
  let t_start
  return function() {
    let that = this,
      args = arguments,
      t_cur = +new Date()
    //先清理上一次的调用触发（上一次调用触发事件不执行）
    clearTimeout(timer)
    //如果不存触发时间，那么当前的时间就是触发时间
    if (!t_start) {
      t_start = t_cur
    }
    //如果当前时间-触发时间大于最大的间隔时间（mustDelay），触发一次函数运行函数
    if (t_cur - t_start >= mustDelay) {
      fn.apply(that, args)
      t_start = t_cur
    } else {
      //否则延迟执行
      timer = setTimeout(() => {
        fn.apply(that, args)
      }, delay)
    }
  }
}
